<template>
  <el-dialog :title="title" :visible="showDialog" @close="cancel">
    <el-form ref="addDept" :model="newDept" label-width="120px" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="newDept.name" placeholder="1-50个字符" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="newDept.code" placeholder="1-50个字符" style="width: 80%" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="newDept.manager" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="manager in managers" :key="manager.id" :label="manager.username" :value="manager.username" style="width: 80%" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="newDept.introduce" placeholder="1-300个字符" type="textarea" :rows="3 " style="width: 80%" />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="info" size="small" @click="cancel">取消</el-button>
        <el-button type="primary" size="small" @click="success">确认</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, getEmployeeSimple, addDept, getDeptInfo, editDept } from '@/api/departments'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    node: {
      type: Object,
      default: null
    }
  },
  data() {
    const checkName = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.newDept.id) {
        isRepeat = depts.filter(item => item.id !== this.newDept.id && item.pid === this.newDept.pid).some(item => value === item.name)
      } else {
        isRepeat = depts.filter(item => item.pid === this.node.id).some(item => item.name === value)
      }
      isRepeat ? callback(new Error('部门名重复')) : callback()
    }
    const checkCode = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let isRepeat = false
      if (this.newDept.id) {
        isRepeat = depts.filter(item => item.id !== this.newDept.id).some(item => value === item.code && value)
      } else {
        isRepeat = depts.some(item => item.code === value && value)
      }
      console.log(isRepeat)
      isRepeat ? callback(new Error('code repeat')) : callback()
    }
    return {
      newDept: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      managers: [],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '该项是必填项哦' }, { min: 1, max: 50, trigger: 'blur', message: '需要1-50个字符' }, { trigger: 'blur', message: '不能有重复', validator: checkName }],
        code: [{ required: true, trigger: 'blur', message: '该项是必填项哦' }, { min: 1, max: 50, trigger: 'blur', message: '需要1-50个字符' }, { trigger: 'blur', message: 'code不能有重复', validator: checkCode }],
        manager: [{ required: true, trigger: 'blur', message: '该项是必填项哦' }],
        introduce: [{ required: true, trigger: 'blur', message: '该项是必填项哦' }, { min: 1, max: 300, trigger: 'blur', message: '需要1-300个字符' }]
      }
    }
  },
  computed: {
    title() {
      return this.newDept.id ? '编辑部门' : '新增子部门'
    }
  },
  methods: {
    cancel() {
      this.newDept = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$refs.addDept.resetFields() // 重置校验字段
      this.$emit('cancel') // 关闭dailog的传统写法
      // this.$emit('updata:showDialog', false)
    },
    success() {
      this.$refs.addDept.validate(async isOk => {
        if (isOk) {
          // 调用新增接口
          if (this.newDept.id) {
            await editDept(this.newDept)
          } else {
            await addDept({ ...this.newDept, pid: this.node.id })
          }
          this.$emit('success')
          // this.$emit('updata:showDialog', false) // 借助sync修饰符修改props
        }
      })
    },
    async getEmployeeSimple() {
      const res = await getEmployeeSimple()
      // console.log(res)
      this.managers = res
    },
    async getDeptInfo() {
      this.newDept = await getDeptInfo(this.node.id)
    }
  }
}
</script>

<style>

</style>
